package Engine.loader.obj;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import org.lwjgl.examples.Game;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

import Game.GameObject;

import static org.lwjgl.opengl.GL11.*;

public class ModelObj extends GameObject {
	public ArrayList<Vertex> vertex;
	public ArrayList<VertexNormal> vertexNormal;
	public ArrayList<VertexTexture> vertexTexture;
	public ArrayList<Face> face;
	public String groupId = new String();
	public String materialId = new String();
	private Texture textures;
	public int firstFace;
	public int lastFace;

	public ModelObj(ModelObj obj) {
		super(obj.x, obj.y, obj.z);
		
		this.face = (ArrayList<Face>) obj.face.clone();
		this.vertex = (ArrayList<Vertex>) obj.vertex.clone();
		this.vertexNormal = (ArrayList<VertexNormal>) obj.vertexNormal.clone();
		this.vertexTexture = (ArrayList<VertexTexture>) obj.vertexTexture.clone();
		textures = obj.textures;
		this.firstFace = obj.firstFace;
		this.lastFace = obj.lastFace;
	}
	
	public ModelObj(String texturePath) {
		super(0, 0, 0);
		vertex = new ArrayList<Vertex>();
		vertexNormal = new ArrayList<VertexNormal>();
		vertexTexture = new ArrayList<VertexTexture>();
		face = new ArrayList<Face>();

		try {
			textures = TextureLoader.getTexture("PNG", new FileInputStream(
					texturePath));
		} catch (FileNotFoundException e) {
			System.err.println(e);
		} catch (IOException e) {
			System.err.println(e);
		}

		textures.bind();
	}

	public void update(long diftime, GameObject obj) {
		
	}

	public void render(GameObject obj) {
		glColor3f(1.0f, 1.0f, 1.0f);
		glPushMatrix();
		{	
			glBindTexture(GL_TEXTURE_2D, textures.getTextureID());
//			System.out.println("firstFace" + firstFace);
			for (int i = firstFace; i < face.size(); i++) {
				if (face.get(i).v.length > 3) {
					glBegin(GL_QUADS);
					{
						for (int j = 0; j < 4; j++) {
							try{
								glNormal3f(
										vertexNormal.get(face.get(i).vn[j] - 1).x,
										vertexNormal.get(face.get(i).vn[j] - 1).y,
										vertexNormal.get(face.get(i).vn[j] - 1).z);
	
								glTexCoord2f(
										vertexTexture.get(face.get(i).vt[j]-1).x,
										vertexTexture.get(face.get(i).vt[j]-1).y);
	
								glVertex3f(vertex.get(face.get(i).v[j] - 1).x,
										vertex.get(face.get(i).v[j] - 1).y,
										vertex.get(face.get(i).v[j] - 1).z);
							} catch (Exception e) {
								System.err.println(e);
							}
						}
					}
					glEnd();

				} else {
					
					glBegin(GL_TRIANGLE_STRIP);
					{
						for (int j = 0; j < 3; j++) {
							try {
								glNormal3f(
										vertexNormal.get(face.get(i).vn[j] - 1).x,
										vertexNormal.get(face.get(i).vn[j] - 1).y,
										vertexNormal.get(face.get(i).vn[j] - 1).z);
	
								glTexCoord2f(
										vertexTexture.get(face.get(i).vt[j] - 1).x,
										vertexTexture.get(face.get(i).vt[j] - 1).y);
															
								glVertex3f(vertex.get(face.get(i).v[j] - 1).x,
										vertex.get(face.get(i).v[j] - 1).y,
										vertex.get(face.get(i).v[j] - 1).z);
							} catch (Exception e) {
								System.err.println(e);
							}
						}
					}
					glEnd();
				}
			}
		}
		glBindTexture(GL_TEXTURE_2D, 0);
		glPopMatrix();
	}

	@Override
	public void Update(long diftime) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void Render() {
		// TODO Auto-generated method stub
		
	}
}
